<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog = true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column label="序号" width="120">
                <template
                  slot-scope="scope"
                >{{ (page.page - 1) * page.pagesize + scope.$index + 1 }}
                </template>
              </el-table-column>

              <!-- <el-table-column label="序号" width="120" type="index" /> -->
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="assignPerm(scope.row.id)">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="edit(scope.row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="del(scope.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :page-sizes="[2, 4, 6, 8]"
                :page-size="page.pagesize"
                layout="sizes,total,prev,pager,next,jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="formData.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="formData.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="formData.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="formData.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog :title="title" :visible="showDialog" @close="btnCancel">
        <el-form
          v-if="showDialog"
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button
              size="small"
              type="primary"
              @click="btnOK"
            >确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
    <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
      <!-- 放置一个组件 来显示 权限的树形结构 el-tree  el-table -->
      <el-tree
        ref="permTree"
        :data="permData"
        :props="{
          label: 'name'
        }"
        default-expand-all=""
        :show-checkbox="true"
        check-strictly=""
        node-key="id"
        :default-checked-keys="selectCheck"
      />
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnPermCancel">取消</el-button>
          <el-button size="small" type="primary" @click="btnPermOK">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { transListToTreeData } from '@/utils'
import { getPermissionList, assignPerm } from '@/api/permission'
import {
  getCompanyInfo,
  getRoleList,
  updateRole,
  getRoleDetail,
  deleteRole,
  addRole
} from '@/api/setting'
export default {
  data() {
    return {
      showPermDialog: false,
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' },
          {
            min: 2,
            max: 20,
            message: '角色名称要求2-20个字符',
            trigger: 'blur'
          }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 200,
            message: '角色描述要求1-200个字符',
            trigger: 'blur'
          }
        ]
      },
      formData: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      },
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      list: [],
      permData: [], // 专门用来接收权限数据 树形数据
      selectCheck: [], // 定义一个数组来接收 已经选中的节点
      roleId: '',
      page: {
        page: 1,
        pagesize: 2
      },
      total: 0
    }
  },
  computed: {
    ...mapGetters(['companyId']),
    title() {
      return this.roleForm.id ? '编辑角色' : '添加角色'
    }
  },
  async created() {
    this.getCompanyInfo()
    this.getRoleList()
  },
  methods: {
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
    },
    async getRoleList() {
      const res = await getRoleList(this.page)
      this.list = res.rows
      this.total = res.total
    },
    handleSizeChange(val) {
      this.page.pagesize = val
      this.getRoleList()
    },
    handleCurrentChange(val) {
      this.page.page = val
      this.getRoleList()
    },
    async del(id) {
      await this.$confirm('您是否确定要删除该角色吗')
      await deleteRole(id)
      this.getRoleList()
      this.$message.success('删除成功')
    },
    async edit(id) {
      this.roleForm = await getRoleDetail(id)
      this.showDialog = true // 为了不出现闪烁的问题 先获取数据 再弹出层
    },
    async btnCancel() {
      // 你的表单数据同样要清空
      this.roleForm = {
        name: '',
        description: ''
      }
      // 你的校验同样要重置
      await this.$refs.roleForm.resetFields()
      // 关闭弹层
      this.showDialog = false
    },
    async btnOK() {
      await this.$refs.roleForm.validate()
      // 这里需要提前考虑 新增的场景
      if (this.roleForm.id) {
        // 编辑场景
        await updateRole(this.roleForm)
      } else {
        // 新增
        await addRole(this.roleForm)
      }
      // 重新拉取数据
      await this.getRoleList()
      // 关闭弹层
      this.showDialog = false
      this.$message.success('操作成功')
    },
    async assignPerm(id) {
      this.roleId = id // 记录点击的角色id
      const { permIds } = await getRoleDetail(id) // permIds就是角色所拥有的权限的点
      this.selectCheck = permIds
      const res = await getPermissionList()
      console.log(res)
      this.permData = transListToTreeData(res, '0')
      this.showPermDialog = true
    },
    btnPermCancel() {
      this.selectCheck = [] // 清空选中的节点
      this.showPermDialog = false
    },
    async  btnPermOK() {
      // 调用接口
      // getCheckedKeys 方法 得到是 node-key属性的集合
      await assignPerm({ id: this.roleId, permIds: this.$refs.permTree.getCheckedKeys() })
      this.showPermDialog = false
      this.showPermDialog = false
    }
  }
}
</script>

<style>
</style>
